﻿Imports System
Imports System.Globalization
Imports System.Reflection

Namespace System.Workflow.Activities.Rules
    Friend Class RuleFieldResult
        Inherits RuleExpressionResult
        ' Methods
        Public Sub New(ByVal targetObject As Object, ByVal fieldInfo As FieldInfo)
            If (fieldInfo Is Nothing) Then
                Throw New ArgumentNullException("fieldInfo")
            End If
            Me.targetObject = targetObject
            Me.fieldInfo = fieldInfo
        End Sub


        ' Properties
        Public Overrides Property Value As Object
            Get
                If (Not Me.fieldInfo.IsStatic AndAlso (Me.targetObject Is Nothing)) Then
                    Dim exception As New RuleEvaluationException(String.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullField, New Object() { Me.fieldInfo.Name }))
                    exception.Data.Item("ErrorObject") = Me.fieldInfo
                    Throw exception
                End If
                Return Me.fieldInfo.GetValue(Me.targetObject)
            End Get
            Set(ByVal value As Object)
                If (Not Me.fieldInfo.IsStatic AndAlso (Me.targetObject Is Nothing)) Then
                    Dim exception As New RuleEvaluationException(String.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullField, New Object() { Me.fieldInfo.Name }))
                    exception.Data.Item("ErrorObject") = Me.fieldInfo
                    Throw exception
                End If
                Me.fieldInfo.SetValue(Me.targetObject, value)
            End Set
        End Property


        ' Fields
        Private fieldInfo As FieldInfo
        Private targetObject As Object
    End Class
End Namespace

